package com.fjrcloud.service;

import com.fjrcloud.dao.LandStandardDao;
import com.fjrcloud.entity.Formalities;
import com.fjrcloud.entity.Land;
import com.fjrcloud.entity.LandStandard;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 17-1-18.
 */
@Service
public class LandStandardService {

    @Autowired
    private LandStandardDao standardDao;

    public LandStandard save(LandStandard standard) throws BusinessException {

        if(standard.getType().equals(Land.building) && standard.getFormalities()==null)
            throw new BusinessException("不确定手续的完整情况");

        if(standard.getPattern() && standard.getRatio()==null)
            throw new BusinessException("没有比例数据");

        standard.setCreateTime(System.currentTimeMillis());

        return standardDao.save(standard);
    }
    public LandStandard get(Integer id){
        return standardDao.findOne(id);
    }
    public void del(Integer id){
        standardDao.delete(id);
    }

    /**
     * 所有标准
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<LandStandard> list(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return standardDao.findByValidOrderByCreateTimeAsc(true,pageRequest);
    }

    /**
     * 所有空地标准
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<LandStandard> listClearing(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return standardDao.findByTypeAndValidOrderByCreateTimeAsc(Land.clearing,true,pageRequest);
    }

    /**
     * 所有建筑用地标准
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<LandStandard> listBuilding(Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return standardDao.findByTypeAndValidOrderByCreateTimeAsc(Land.building,true,pageRequest);
    }

    /**
     * 按照手续进行分类
     * @param formalities
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<LandStandard> listBuildingClass(Formalities formalities, Integer pageNum, Integer pageSize){

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return standardDao.findByTypeAndFormalitiesAndValidOrderByCreateTimeAsc(Land.building,formalities,true,pageRequest);
    }

}
